# -*- coding: UTF-8 -*-
from . import BaseController
from settings import logger
from backend.libs.args_util import *
from backend.models import GoodsOrder, VideoOrderState
from backend.libs import myjson as json


class GoodsOrderListController(BaseController):

    def get(self):
        """商品订单列表
        """
        args = PageArgs(self, 'get', 'orderId')
        args.int_arg('state', 1)
        gso = GoodsOrder.objects.filter(state=args.state)

        if args.orderId:
            gso = gso.filter(order_id=args.orderId)

        gso = gso.order_by('-create_time', '-id')

        paginator = self.pagination(gso.all(), number_per_page=args.pageSize)
        return self.render_json(data=paginator)


class ChangeStateController(BaseController):

    def post(self):
        data = json.loads(self.request.body)
        goods_order_id = data.get('id', '')
        state_id = data.get('state')
        if not goods_order_id:
            return render_json(code=1010, msg='参数不能为空')
        gso = GoodsOrder.objects.filter(id=goods_order_id).first()
        if not gso:
            return render_json(code=1011, msg='商品订单不存在')
        video_state = VideoOrderState.objects.filter(id=state_id).first()
        gso.state = video_state
        gso.save()
        return self.render_json(data={})
        